<template>
  <ElDropdown trigger="click" :teleported="false">
    <div class="more-button">
      <i :class="'iconfont icon-xiala'"></i>
    </div>
    <template #dropdown>
      <ElDropdownMenu>
        <ElDropdownItem @click="refresh">
          <ElIcon><Refresh /></ElIcon>{{ $t("tabs.refresh") }}
        </ElDropdownItem>
        <ElDropdownItem @click="maximize">
          <ElIcon><FullScreen /></ElIcon>{{ $t("tabs.maximize") }}
        </ElDropdownItem>
        <ElDropdownItem divided @click="closeCurrentTab">
          <ElIcon><Remove /></ElIcon>{{ $t("tabs.closeCurrent") }}
        </ElDropdownItem>
        <ElDropdownItem @click="tabStore.closeTabsOnSide(route.fullPath, 'left')">
          <ElIcon><DArrowLeft /></ElIcon>{{ $t("tabs.closeLeft") }}
        </ElDropdownItem>
        <ElDropdownItem @click="tabStore.closeTabsOnSide(route.fullPath, 'right')">
          <ElIcon><DArrowRight /></ElIcon>{{ $t("tabs.closeRight") }}
        </ElDropdownItem>
        <ElDropdownItem divided @click="closeOtherTab">
          <ElIcon><CircleClose /></ElIcon>{{ $t("tabs.closeOther") }}
        </ElDropdownItem>
        <ElDropdownItem @click="closeAllTab">
          <ElIcon><FolderDelete /></ElIcon>{{ $t("tabs.closeAll") }}
        </ElDropdownItem>
      </ElDropdownMenu>
    </template>
  </ElDropdown>
</template>

<script setup lang="ts">
import { inject, nextTick } from "vue";
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElIcon } from "element-plus";
import { HOME_URL } from "@/config";
import { useTabsStore } from "@/stores/modules/tabs";
import { useGlobalStore } from "@/stores/modules/global";
import { useKeepAliveStore } from "@/stores/modules/keepAlive";
import { useRoute, useRouter } from "vue-router";

const route = useRoute();
const router = useRouter();
const tabStore = useTabsStore();
const globalStore = useGlobalStore();
const keepAliveStore = useKeepAliveStore();

// refresh current page
const refreshCurrentPage: Function = inject("refresh") as Function;
const refresh = () => {
  setTimeout(() => {
    keepAliveStore.removeKeepAliveName(route.name as string);
    refreshCurrentPage(false);
    nextTick(() => {
      keepAliveStore.addKeepAliveName(route.name as string);
      refreshCurrentPage(true);
    });
  }, 0);
};

// maximize current page
const maximize = () => {
  globalStore.setGlobalState("maximize", true);
};

// Close Current
const closeCurrentTab = () => {
  if (route.meta.isAffix) return;
  tabStore.removeTabs(route.fullPath);
  keepAliveStore.removeKeepAliveName(route.name as string);
};

// Close Other
const closeOtherTab = () => {
  tabStore.closeMultipleTab(route.fullPath);
};

// Close All
const closeAllTab = () => {
  tabStore.closeMultipleTab();
  router.push(HOME_URL);
};
</script>

<style scoped lang="scss">
@import "../index.scss";
</style>
